package com.controller;


import com.bean.dto.Dto;
import com.bean.pojo.ItripHotel;
import com.bean.vo.hotel.HotCityBySearchHotelVo;
import com.bean.vo.hotel.SearchHotCityVO;
import com.bean.vo.hotel.SearchHotelVO;
import com.service.HotelListControllerService;
import com.util.DtoUtil;
import com.util.PageAssistant;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.noggit.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class HotelListController {

    @Autowired
    private SolrClient solrClient;
    @Autowired
    private HotelListControllerService hotelControllerService;

    @PostMapping("/all")
    private Object query(SearchHotelVO searchHotelVO) {
        System.out.println(">>查询全部");
        System.out.println(searchHotelVO);

        SolrQuery query = new SolrQuery("*:*");

        try {
            //执行查询获取响应
            QueryResponse response = solrClient.query(query);
            //用过查询响应获取solr集合
            SolrDocumentList documentList = response.getResults();

            String json = JSONUtil.toJSON(documentList);
            return documentList;
        } catch (Exception e) {
            e.printStackTrace();
            return "查询失败";
        }

    }


    @PostMapping("/api/hotellist/searchItripHotelListByHotCity")
    public
    @ResponseBody Dto searchItripHotelListByHotCity(@RequestBody SearchHotCityVO searchHotCityVO) {
        System.out.println(">>> 根据热门城市查询酒店");
        System.out.println(searchHotCityVO);

        try {
            List<HotCityBySearchHotelVo> searchHotCityVOList = hotelControllerService.searchItripHotelListByHotCity(searchHotCityVO);

            return DtoUtil.returnSuccess("查询成功", searchHotCityVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnError("查询失败");
        }
    }

    @PostMapping("/api/hotellist/searchItripHotelPage")
    public @ResponseBody Dto searchItripHotelPage(@RequestBody SearchHotelVO searchHotelVO,PageAssistant<HotCityBySearchHotelVo> pa) {
        System.out.println(">>> 查询酒店分页VO");
        System.out.println(searchHotelVO);
        System.out.println(">>> 查询酒店分页pa");
        System.out.println(pa);
        try {
            PageAssistant<HotCityBySearchHotelVo> searchHotelVOList = hotelControllerService.searchItripHotelPage(searchHotelVO, pa);

            return DtoUtil.returnSuccess("查询成功", searchHotelVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnError("查询失败");
        }
    }
}
